package com.group3.service.impl;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.AttendanceResultMapper;
import com.group3.dao.LessonTeacherMapper;
import com.group3.dao.PunchRecordMapper;
import com.group3.domain.*;
import com.group3.pojo.receive.AttendanceReceive;
import com.group3.pojo.receive.RecordReceive;
import com.group3.pojo.result.AttendanceInfoResult;
import com.group3.service.TeacherAttendanceService;
import com.group3.utils.JWTUtils;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * TeacherAttendanceServiceImpl
 *
 * @author HY,MHH,PX
 * 2021/06/10 16:53
 **/


@Service
public class TeacherAttendanceServiceImpl implements TeacherAttendanceService {
    @Resource
    PunchRecordMapper punchRecordMapper;
    @Resource
    private AttendanceResultMapper attendanceResultMapper;
    @Resource
    private LessonTeacherMapper lessonTeacherMapper;

    @Override
    public Result<List<PunchRecord>> selectRecord(RecordReceive recordReceive) {
        //1.new一个对应的example
        PunchRecordExample example = new PunchRecordExample();
        PunchRecordExample.Criteria criteria = example.createCriteria();

        //2.为example添加查询条件
//          打卡时间在查询的开始结束时间之间
        if (NotNull.judge(recordReceive.getStartDate()) && NotNull.judge(recordReceive.getEndDate())) {
            criteria.andPunchTimeBetween(recordReceive.getStartDate(), recordReceive.getEndDate());
        }
//          学生id
        if (NotNull.judge(recordReceive.getStudentId())){
            criteria.andStudentIdEqualTo(recordReceive.getStudentId());
        }
//          学生
        if (NotNull.judge(recordReceive.getStudentName())){
            criteria.andStudentNameLike("%" + recordReceive.getStudentName() + "%");
        }


//        1.先开启分页
        PageHelper.startPage(recordReceive.getPageNumber(), recordReceive.getPageSize());

//        2.执行SQL语句
        List<PunchRecord> punchRecord = punchRecordMapper.selectByExample(example);

//        3.获取分页信息
        PageInfo<PunchRecord> PunchRecordPageInfo = new PageInfo<>(punchRecord);

        return ResultUtil.success("查询成功", punchRecord, PunchRecordPageInfo.getTotal());
    }

    @Override
    public Result<List<AttendanceResult>> selectAttendance(AttendanceReceive attendanceReceive,String token) {
        DecodedJWT jwt = JWTUtils.getToken(token);//获取token
        String teacherId = jwt.getClaim("id").asString();//获取登录老师的id

        //查询老师所教课程集合<老师id,课程名>
//        LessonTeacherExample lessonTeacherExampleTemp = new LessonTeacherExample();
//        lessonTeacherExampleTemp.createCriteria().andTeacherIdEqualTo(teacherId);
//        List<LessonTeacherKey> keys = lessonTeacherMapper.selectByExample(lessonTeacherExampleTemp);
//        // 取出课程名集合
//        List<String> lessonNameByT = new ArrayList<>();
//        for (LessonTeacherKey key : keys) {
//            lessonNameByT.add(key.getLessonName());
//        }

        AttendanceResultExample attendanceResultExample = new AttendanceResultExample();
        AttendanceResultExample.Criteria criteria = attendanceResultExample.createCriteria();

        // 只查询自己所教的课的记录
//        criteria.andTeacherIdEqualTo(teacherId);

        //时间范围
        if (NotNull.judge(attendanceReceive.getStartTime()) &&
                NotNull.judge(attendanceReceive.getEndTime())){
            criteria.andDateBetween(attendanceReceive.getStartTime()
                    ,attendanceReceive.getEndTime());
        }

        // 院系
        if (NotNull.judge(attendanceReceive.getDepartments())){
            criteria.andDepartNameEqualTo(attendanceReceive.getDepartments());
        }

        // 专业
        if (NotNull.judge(attendanceReceive.getProfessional())){
            criteria.andProfessionNameEqualTo(attendanceReceive.getProfessional());
        }

        // 年级
        if (NotNull.judge(attendanceReceive.getGrade())){
            criteria.andGradeEqualTo(attendanceReceive.getGrade());
        }

        // 班级
        if (NotNull.judge(attendanceReceive.getKlass())){
            criteria.andClassIdEqualTo(attendanceReceive.getKlass());
        }

        // 课程名
        if (NotNull.judge(attendanceReceive.getLessonName())){
            criteria.andLessonNameEqualTo(attendanceReceive.getLessonName());
        }

        // 学生id
        if (NotNull.judge(attendanceReceive.getStudentId())){
            criteria.andStudentIdEqualTo(attendanceReceive.getStudentId());
        }

        // 学生姓名
        if (NotNull.judge(attendanceReceive.getStudentName())){
            criteria.andStudentNameLike("%"+(attendanceReceive.getStudentName())+"%");
        }

        // 考勤状态
        if (NotNull.judge(attendanceReceive.getType())){
            criteria.andAttendanceTypeEqualTo(attendanceReceive.getType());
        }


//        //根据实训老师查询课程（lesson_teacher表）
//        LessonTeacherExample lessonTeacherExample = new LessonTeacherExample();
//        lessonTeacherExample.setDistinct(true);
////
////        // TODONE 老师查询考勤时，该teacherId为查询老师的id（andTeacherIdEqualTo）
//        lessonTeacherExample.createCriteria()
//                .andTeacherIdEqualTo(teacherId);




//        1.先开启分页
        PageHelper.startPage(attendanceReceive.getPageNumber(), attendanceReceive.getPageSize());//启动分页
//        2.执行SQL语句
        List<AttendanceInfoResult> attendanceResults = attendanceResultMapper.selectAttendanceInfo(attendanceResultExample);
//        3.获取分页信息
        PageInfo<AttendanceInfoResult> attendanceInfoResultPageInfo = new PageInfo<>(attendanceResults);//获取分页信息

        return ResultUtil.success("查询成功", attendanceResults, attendanceInfoResultPageInfo.getTotal());
    }

    @Override
    public Result<AttendanceResult> showAttendance(Integer leaveId,String studentId) {
        AttendanceResultExample attendanceResultExample = new AttendanceResultExample();
        attendanceResultExample.createCriteria().andLeaveIdEqualTo(leaveId);

//        AttendanceResult attendanceResult = attendanceResultMapper.selectByExample(attendanceResultExample);

        return null;
    }
}
